home *** CD-ROM | disk | FTP | other *** search
- Path: CES.CWRU.Edu!calvitti
- From: calvitti@yuggoth.ces.cwru.edu (AC)
- Newsgroups: sci.math.num-analysis,comp.lang.c++
- Subject: [Q] Moving invariant code out of loops?
- Followup-To: sci.math.num-analysis
- Date: 23 Feb 1996 00:10:50 GMT
- Organization: Case Western Reserve University
- Distribution: inet
- Message-ID: <CALVITTI.96Feb22191050@yuggoth.ces.cwru.edu>
- NNTP-Posting-Host: yuggoth.ces.cwru.edu
-
- i was wondering if any C/C++ compilers can move invariant code out of
- loops, like the assert calls in operations like the following
- simple example:
-
- ...
-
- //////////////////////////////////////////
- inline double Vector::operator[](int i)
- {
- assert(i >= 0 && i < size());
- return data_array[i];
- }
-
- // ... assume friend of Vector
- //////////////////////////////////////////
- double inner_product(Vector& a, Vector& b)
- {
- double tmp = 0;
- assert(a.size() == b.size());
- for(int i=0;i<a.size();i++)
- tmp += a[i] * b[i];
- return tmp;
- }
-
- since the index 'i' in 'inner_product' does not violate the assertion
- called in 'Vector::operator[]', the latter imposes redundant overhead
- on the routine. can/do (C/C++) compilers optimize this away? IMO, this
- seems a hard problem in general, since for example, the 'b[i]'
- invariance assumes not only the structure of that for loop but also
- that 'a.size() == b.size()'.
-
- of course, an inelegant solution is to define an alternative
- (protected or private) element getter which does not call 'assert',
- but that, imo, is inelegant and unsafe, since a lot of time and effort
- is spent writing member functions (or friends) themselves.
-
- also, are exception mechanisms faster for this kind of functionality?
-
- if not, are there languages/implementations that can do this?
-
- thanks for the information,
-
- alan
-
- +---------------------------------+
- | Alan Calvitti |
- | Control Engineering |
- | Case Western Reserve University |
- +---------------------------------+
-
-
-